home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / standardize.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  99 lines

  1. ; $Id: standardize.pro,v 1.6 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1996-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       STANDARDIZE
  8. ;
  9. ; PURPOSE:
  10. ;       This function computes standardized variables from an array 
  11. ;       of M variables (columns) and N observations (rows). The result
  12. ;       is an M-column, N-row array where all columns have a mean of
  13. ;       zero and a variance of one.
  14. ;
  15. ; CATEGORY:
  16. ;       Statistics
  17. ;
  18. ; CALLING SEQUENCE:
  19. ;       Result = Standardize(A)
  20. ;
  21. ; INPUTS:
  22. ;       A:    An M-column, N-row array of type float or double.
  23. ;
  24. ; KEYWORD PARAMETERS:
  25. ;       DOUBLE:  If set to a non-zero value, computations are done in
  26. ;                double precision arithmetic.
  27. ;
  28. ; EXAMPLE:
  29. ;       Define an array with 4 variables and 20 observations.
  30. ;         array = $
  31. ;           [[19.5, 43.1, 29.1, 11.9], $
  32. ;            [24.7, 49.8, 28.2, 22.8], $
  33. ;            [30.7, 51.9, 37.0, 18.7], $
  34. ;            [29.8, 54.3, 31.1, 20.1], $
  35. ;            [19.1, 42.2, 30.9, 12.9], $
  36. ;            [25.6, 53.9, 23.7, 21.7], $
  37. ;            [31.4, 58.5, 27.6, 27.1], $
  38. ;            [27.9, 52.1, 30.6, 25.4], $
  39. ;            [22.1, 49.9, 23.2, 21.3], $
  40. ;            [25.5, 53.5, 24.8, 19.3], $
  41. ;            [31.1, 56.6, 30.0, 25.4], $
  42. ;            [30.4, 56.7, 28.3, 27.2], $
  43. ;            [18.7, 46.5, 23.0, 11.7], $
  44. ;            [19.7, 44.2, 28.6, 17.8], $
  45. ;            [14.6, 42.7, 21.3, 12.8], $
  46. ;            [29.5, 54.4, 30.1, 23.9], $
  47. ;            [27.7, 55.3, 25.7, 22.6], $
  48. ;            [30.2, 58.6, 24.6, 25.4], $
  49. ;            [22.7, 48.2, 27.1, 14.8], $
  50. ;            [25.2, 51.0, 27.5, 21.1]]
  51. ;
  52. ;       Compute the mean and variance of each variable using the MOMENT 
  53. ;       function. Note: The skewness and kurtosis are also computed.
  54. ;         IDL> for k = 0, 3 do print, MOMENT(array[k,*])
  55. ;               25.3050      25.2331    -0.454763     -1.10028
  56. ;               51.1700      27.4012    -0.356958     -1.19516
  57. ;               27.6200      13.3017     0.420289     0.104912
  58. ;               20.1950      26.0731    -0.363277     -1.24886
  59. ;
  60. ;       Compute the standardized variables.
  61. ;         IDL> result = STANDARDIZE(array)
  62. ;
  63. ;       Compute the mean and variance of each standardized variable using 
  64. ;       the MOMENT function. Note: The skewness and kurtosis are also computed.
  65. ;         IDL> for k = 0, 3 do print, MOMENT(result[k,*])
  66. ;                -7.67130e-07      1.00000    -0.454761     -1.10028
  67. ;                -3.65451e-07      1.00000    -0.356958     -1.19516
  68. ;                -1.66707e-07      1.00000     0.420290     0.104913
  69. ;                 4.21703e-07      1.00000    -0.363278     -1.24886
  70. ;
  71. ; MODIFICATION HISTORY:
  72. ;           Written by:  GGS, RSI, February 1996
  73. ;-
  74.  
  75. FUNCTION Standardize, X, Double = Double
  76.  
  77.   ON_ERROR, 2
  78.  
  79.   Sx = SIZE(X)
  80.  
  81.   if Sx[Sx[0]+1] ne 4 and Sx[Sx[0]+1] ne 5 then $
  82.     MESSAGE, "Input array must be float or double."
  83.  
  84.   if N_ELEMENTS(Double) eq 0 then Double = (Sx[Sx[0]+1] eq 5)
  85.  
  86.   if Sx[0] eq 2 then begin ;Standardize the columns of X.
  87.     xSTD = FLTARR(Sx[1], Sx[2], /nozero) ;Output array.
  88.     if Double ne 0 then xSTD = DOUBLE(xSTD)
  89.     no = Sx[2]            ;# of observations
  90.     mean = TOTAL(X, 2, DOUBLE = Double) / no ;Vector of Means
  91.     xstd = X - (mean # REPLICATE(1, No))     ;Deviations from means
  92.     stdev = SQRT(TOTAL(xstd^2, 2, Double = Double) / (No-1)) ;Vector of Stdevs
  93.     if Double eq 0 then RETURN, FLOAT(xstd * ((1./stdev) # replicate(1, No))) $
  94.     else RETURN, xstd * ((1./stdev) # replicate(1, No)) ;Normalize by 1./Stdev
  95.   endif $
  96.   else MESSAGE, "Input array must be two-dimensional."
  97.  
  98. END
  99.